package cn.edu.cdu.argorithm.impl;

import cn.edu.cdu.argorithm.ILRUCache;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

public class LRUCacheSolutions {
    public static class Solution1 extends LinkedHashMap<Integer, Integer> implements ILRUCache {
        private final int capacity;
        public Solution1(int capacity) {
            super(capacity, .75f, true);
            this.capacity = capacity;
        }

        @Override
        public int get(int key) {
            return getOrDefault(key , -1);
        }

        @Override
        public void put(int key, int value) {
            super.put(key, value);
        }

        @Override
        protected boolean removeEldestEntry(Map.Entry<Integer, Integer> eldest) {
            return size() > capacity;
        }
    }

    public static class Solution2 implements ILRUCache{
        class DLinkedNode{
            int key;
            int val;
            DLinkedNode prev;
            DLinkedNode next;

            public DLinkedNode(){}

            public DLinkedNode(int key, int val) {
                this.key = key;
                this.val = val;
            }
        }

        private Map<Integer, DLinkedNode> cache;
        private int capacity;
        private int size;
        private DLinkedNode head, tail;

        public Solution2(int capacity) {
            cache = new HashMap<>();
            head = new DLinkedNode();
            tail = new DLinkedNode();
            head.next = tail;
            tail.prev = head;
            this.capacity = capacity;
            this.size = 0;
        }
        @Override
        public int get(int key) {
            DLinkedNode node = cache.get(key);
            if(node == null) {
                return -1;
            }
            moveToHead(node);
            return node.val;
        }

        @Override
        public void put(int key, int value) {
            if(!cache.containsKey(key)) {
                DLinkedNode node = new DLinkedNode(key, value);
                cache.put(key, node);
                addToHead(node);
                size++;
                if(size > capacity) {
                    DLinkedNode removed = removeTail();
                    cache.remove(removed.key);
                    size--;
                }
            } else {
                DLinkedNode node = cache.get(key);
                node.val = value;
                moveToHead(node);
            }
        }


        private void removeNode(DLinkedNode node) {
            node.prev.next = node.next;
            node.next.prev = node.prev;
        }

        private void moveToHead(DLinkedNode node) {
            removeNode(node);
            addToHead(node);
        }

        private void addToHead(DLinkedNode node) {
            node.prev = head;
            node.next = head.next;
            head.next.prev = node;
            head.next = node;
        }

        private DLinkedNode removeTail() {
            DLinkedNode node = tail.prev;
            removeNode(node);
            return node;
        }
    }
}
